package com.sto.stoapp.activity.sales;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SoleAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.entity.FunctionEntity;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

public class SalesPhoneRecvActivity extends StoHomeActivity {
	private ListView PhoneRecvFuncList;
	private SoleAdapter PhoneRecvListAdapter;
	private String busiType = "";
	private final int EXTRACT_RESULT = 0xfd;
	private Boolean OrderIsFinish = false;
	private String titleType = "普通收件";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sto_sales_phone_recv_menu_list);
		this.setTitleBottom(R.string.phone_recv,
				HomeBroadAction.SALES_MAN_BROAD);
		Bundle extras = getIntent().getExtras();
		busiType = extras.getString("busiType");
		titleType = extras.getString("titleType");
		if(titleType ==null ){
			titleType = "普通收件";
		}
		initWidget();
	}

	private void initWidget() {
		PhoneRecvFuncList = (ListView) findViewById(R.id.phoneReceivedFunc_list);
		PhoneRecvListAdapter = new SoleAdapter(this, getResourceList());
		PhoneRecvFuncList.setAdapter(PhoneRecvListAdapter);

		PhoneRecvFuncList.setOnItemClickListener(new StoOnItemClickListener(
				this) {

			@Override
			public void KcOnItemClickListener(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				arg1.setSelected(true);
				TextView title = (TextView) arg1.findViewById(R.id.res_test);
				clickItemEvent(title.getText().toString());
			}

		});
	}

	private void clickItemEvent(String title) {
		// 单票收件
		if (title.equals(getText(R.string.one_recv))) {
			if (!OrderIsFinish) {
				Intent intent = new Intent(this,
						SalesPhoneRecvContentActivity.class);
				intent.putExtra("title", R.string.one_recv);
				intent.putExtra("busiType", busiType);
				intent.putExtra("titleType",titleType);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				// startActivity(intent);
				startActivityForResult(intent, EXTRACT_RESULT);
			} else {
				Helper.showSoundToast(this, "该订单已完成,请返回!", false);
			}
		}
		// 多票收件
		else if (title.equals(getText(R.string.more_recv))) {
			if (!OrderIsFinish) {
				Intent intent = new Intent(this,
						SalesPhoneRecvContentActivity.class);
				intent.putExtra("title", R.string.more_recv);
				intent.putExtra("busiType", busiType);
				intent.putExtra("titleType",titleType);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				// startActivity(intent);
				startActivityForResult(intent, EXTRACT_RESULT);
			} else {
				Helper.showSoundToast(this, "该订单已完成,请返回!", false);
			}
		} else {

		}
	}

	private List<FunctionEntity> getResourceList() {
		List<String> resList = new ArrayList<String>();
		resList.add(getText(R.string.one_recv).toString());
		resList.add(getText(R.string.more_recv).toString());

		List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
		for (String res : resList) {
			FunctionEntity entity = new FunctionEntity();
			entity.setTitle(res);
			functionList.add(entity);
		}
		return functionList;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == EXTRACT_RESULT && resultCode == RESULT_OK) {
			Intent intent = new Intent();
			// 已经提取,且完成
			OrderIsFinish = true;
			setResult(RESULT_OK, intent);
			// String value = data.getStringExtra("Name");
		} else if (requestCode == EXTRACT_RESULT
				&& resultCode == RESULT_FIRST_USER) {
			// 已提取,未完成
			Intent intent = new Intent();
			setResult(RESULT_FIRST_USER, intent);
		} else {

		}
	}

	@Override
	protected void onDestroy() {
		PhoneRecvFuncList = null;
		super.onDestroy();
	}

}
